package com.ssm.controller.admin;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ssm.controller.SSMController;
import com.ssm.exception.ExceptionCode;
import com.ssm.exception.SsmException;
import com.ssm.exception.WebException;
import com.ssm.model.bean.SsmActions;
import com.ssm.model.bean.SsmGroups;
import com.ssm.model.bean.SsmUserGroup;
import com.ssm.model.bean.SsmUserRules;
import com.ssm.model.mapper.ActionMapper;
import com.ssm.model.mapper.GroupsMapper;
import com.ssm.model.mapper.UserGroupMapper;
import com.ssm.model.mapper.UserRuleMapper;

import tk.mybatis.mapper.entity.Example;

/*
 * SSM Controller用户组类
* @author WTC E-mail:546895488@qq.com 
* @version 创建时间：2016年2月5日 下午12:11:46 
* 类说明 
*/
/**
 * @author WTC E-mail:546895488@qq.com 
 * @version 创建时间：2016年2月5日 下午12:11:46 
 */
@Controller
@RequestMapping(value="Group")
public class UserGroupController extends AdminBaseController<SsmGroups> {
	GroupsMapper gm;

	
	/**
	 * @author WTC E-mail:546895488@qq.com 
	 * @version 创建时间：2016年2月5日 下午12:16:12 
	 */
	@RequestMapping(value="index")
	public String UserGroup(HttpServletRequest request)
	{
		this.List2Table(GroupsMapper.class,null,request,false);	
		request.setAttribute("WEB_TITLE", "用户组管理界面");
		return "Group/index";
	}
	/**
	 * 用户组修改
	 * @param request
	 * @param response
	 */
	@RequestMapping(value="edit")
	public String ActionEdit(HttpServletRequest request,SsmGroups group)
	{
		gm=M.getMapper(GroupsMapper.class);
		if(request.getMethod()=="POST")
		{
			if(group.getId()==null)//添加
			{
				group.setCreatetime(this.nowTime().toString());
				String[] rules=request.getParameterValues("rules");
				String addrules="";
				for(String temp:rules){
					addrules=temp+"%"+addrules;
				}
			    gm=M.getMapper(GroupsMapper.class);
			    //设置默认用户组状态
			    group.setStatus(S.getInteger("enusergroup"));
			    group.setRule(addrules);
			    gm.insert(group);
			    request.setAttribute("WEB_TITLE", "用户组添加成功");
				return "redirect:index";
			}
			else//更新
			{
				String[] rules=request.getParameterValues("rules");
				String addrules="";
				for(String temp:rules){
					addrules=temp+"%"+addrules;
				}
			    gm=M.getMapper(GroupsMapper.class);
			    group.setRule(addrules);
				gm.updateByPrimaryKeySelective(group);
				request.setAttribute("WEB_TITLE", "修改用户组成功！");
				//return "Action/index";
				return "redirect:index";
			}
			
		}
		else//编辑页面
		{
			Integer id=Integer.parseInt(request.getParameter("groupid"));
			if(0==id)
			{
				request.setAttribute("WEB_TITLE", "用户组添加界面");
				UserRuleMapper urm=M.getMapper(UserRuleMapper.class);
				List<SsmUserRules> list=urm.selectAll();
				request.setAttribute("list",list);
				return "Group/edit";
				
			}
			else
			{
				group=gm.selectByPrimaryKey(id);
				UserRuleMapper urm=M.getMapper(UserRuleMapper.class);
				List<SsmUserRules> list=urm.selectAll();
				request.setAttribute("list",list);
				request.setAttribute("group",group);
				request.setAttribute("WEB_TITLE", "修改用户组");
				return "Group/edit";
			}
		}
	}
	
	@RequestMapping(value="changeStatus")
	public String changeStatus(HttpServletRequest request)
	{
		changeStatus(GroupsMapper.class,request);
		return "redirect:index";
	}
}
